package com.hhd.flow.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.hhd.flow.engine.support.AttachMentType;
import com.hhd.flow.engine.support.DefaultFlowResource;
import com.hhd.flow.engine.utils.CommonUtils;
import com.hhd.flow.entity.FlowAttachment;
import com.hhd.flow.vo.CompleteUserTaskVo;
import com.hhd.flow.vo.Result;
import com.hhd.flow.engine.FlowEngine;
import com.hhd.flow.engine.cmd.impl.CompleteNotifyCommand;
import com.hhd.flow.engine.cmd.impl.CompleteUserTaskCommand;
import com.mysql.cj.util.Base64Decoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayInputStream;
import java.util.Base64;

@RestController
@RequestMapping("/flow")
public class CompleteController {
    @Autowired
    FlowEngine flowEngine;

    @Transactional
    @PostMapping("/complete")
    public Result complete(@RequestBody CompleteUserTaskVo completeUserTaskVo) {
        resolveUserSign(completeUserTaskVo);
        flowEngine.getFlowActionLogService().logForCompleteProcess(completeUserTaskVo.getFlowUserTaskId(), completeUserTaskVo.getComment());

        CompleteUserTaskCommand userTaskCommand = flowEngine.newCompleteUserTaskCommandBuilder()
                .flowUserTaskId(completeUserTaskVo.getFlowUserTaskId())
                .formModel(completeUserTaskVo.getFormModel())
                .comment(completeUserTaskVo.getComment())
                .userSign(completeUserTaskVo.getUserSign())
                .userManualSelect(completeUserTaskVo.getUserManualSelect())
                .attachmentIds(completeUserTaskVo.getAttachmentIds())
                .commentInfo(completeUserTaskVo.getCommentInfo())
                .approveType(completeUserTaskVo.getApproveType()).build();
        flowEngine.execute(userTaskCommand);
        flowEngine.getFlowActionLogService().logForPassProcess(completeUserTaskVo.getFlowUserTaskId());
        return Result.ok();
    }

    private void resolveUserSign(CompleteUserTaskVo completeUserTaskVo) {
        String userSign = completeUserTaskVo.getUserSign();
        if (!CommonUtils.isEmpty(userSign) && userSign.startsWith("data:image/png")) {
            String[] split = userSign.split(",");
            byte[] decode = Base64.getDecoder().decode(split[1]);
            DefaultFlowResource defaultFlowResource = new DefaultFlowResource();
            defaultFlowResource.setName("user_sign_" + IdWorker.getIdStr() + ".png");
            defaultFlowResource.setInputStream(new ByteArrayInputStream(decode));
            FlowAttachment flowAttachment = flowEngine.upload(defaultFlowResource, AttachMentType.userSign);
            flowEngine.getFlowAttachmentService().save(flowAttachment);
            completeUserTaskVo.setUserSign(flowAttachment.getId());
        }
    }

    @Transactional
    @GetMapping("/completeNotify")
    public Result completeNotify(String flowUserTaskId) {
        CompleteNotifyCommand command = flowEngine.newCompleteNotifyCommandBuilder()
                .flowUserTaskId(flowUserTaskId)
                .build();
        flowEngine.getFlowActionLogService().logForCompleteProcess(flowUserTaskId, "");
        flowEngine.execute(command);
        return Result.ok();
    }
}
